/*  JEVEMon, A Java-based EVEMon clone.
Copyright (C) 2011, Raul Ruiz

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jeveme;

import eveapi.EVEApiConnector;
import javax.swing.JLabel;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;



/**
 *
 * @author ruizr
 */
public class UpdateServerStatus implements Runnable {
    

    /**
     * 
     */
    private JLabel serverStatusLabel;
    
    /**
     * 
     * @param label 
     */
    public UpdateServerStatus(JLabel label) {
        serverStatusLabel = label;
    }
    
    /**
     * 
     */
    public void run() {
            String server_status_xml = EVEApiConnector.getServerStatusXML();
            
            if(server_status_xml.equals("error retrieving server status")) {
                System.out.println("error");
                this.serverStatusLabel.setText("Tranquility Server Status Unknown");
                return;
            }
            
            else { //Parse the XML Response
                try {
                    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder builder = domFactory.newDocumentBuilder();
                    org.xml.sax.InputSource is = new org.xml.sax.InputSource();
                    is.setCharacterStream(new java.io.StringReader(server_status_xml));
                    Document doc = builder.parse(is);
                    XPath xpath = XPathFactory.newInstance().newXPath();
                    
                    // XPath Query for showing all nodes value
                    XPathExpression server_open_expr = xpath.compile("//serverOpen/text()");
                    XPathExpression online_players_expr = xpath.compile("//onlinePlayers/text()");
                    
                    Object server_result = server_open_expr.evaluate(doc, XPathConstants.NODESET);
                    Object players_result = online_players_expr.evaluate(doc, XPathConstants.NODESET);
                
                    NodeList server_nodes = (NodeList) server_result;
                    NodeList player_nodes = (NodeList) players_result;
                    
                    String server_up = server_nodes.item(0).getNodeValue();
                    String players_online = player_nodes.item(0).getNodeValue();
                    
                    String status_text = "Tranquility Server ";
                    if(server_up.equals("True")) {
                        status_text += "Online";
                        status_text += " (" + players_online +" Pilots)";
                    }
                    else {
                        status_text += "Offline";
                    }
                    this.serverStatusLabel.setText(status_text);
                    
                }
                catch(Exception e) {}
            }
            
            
     }

}
